home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre4.z / postgre4 / src / test / testbuf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-27  |  5.3 KB  |  232 lines

  1. static    char    testbuf_c[] = "$Header: /private/postgres/src/test/RCS/testbuf.c,v 1.9 1992/03/04 14:10:52 hong Exp $";
  2.  
  3. #include <stdio.h>
  4.  
  5. #include "postgres.h"
  6. #include "skey.h"
  7. #include "fmgr.h"
  8. #include "htup.h"
  9. #include "log.h"
  10. #include "rlock.h"
  11.  
  12. #include "buf.h"
  13. #include "bufmgr.h"
  14. #include "itemptr.h"
  15. #include "tqual.h"
  16.  
  17. #include "storage/smgr.h"
  18.  
  19. /*
  20.  *    testbuf.c    - code to test the buffer code
  21.  *
  22.  *    Tuples are inserted in one of two formats:
  23.  *        < odd_pos_int, NULL, NULL, NULL >
  24.  *        < even_pos_int, NULL, "12345671234567123...", 0xfe9a >
  25.  */
  26.  
  27. #define    NATTS    (4)
  28. #define    ENDVAL    0xfe9a
  29.  
  30. static    char    Relname[] = "testbufrel";
  31.  
  32. static    struct    attribute a1 =
  33.     {0l, "testbufatt", 23, 0l, 0l, 0l, 4, 1, 0, '\001', '\001'};
  34. static    struct    attribute a2 =
  35.     {0l, "testbufnull", 25, 0l, 0l, 0l, -1, 2, 0, '\000', '\001'};
  36. static    struct    attribute a3 =
  37.     {0l, "testbufvar", 25, 0l, 0l, 0l, -1, 3, 0, '\000', '\001'};
  38. static    struct    attribute a4 =
  39.     {0l, "testbufpost", 21, 0l, 0l, 0l, 2, 4, 0, '\001', '\001'};
  40. static    struct    attribute *Att[NATTS] = { &a1, &a2, &a3, &a4 };
  41.  
  42. static    char            Null[] = " n  ";
  43. static    struct    reldesc        *Reldesc;
  44. static    struct    outdom {
  45.     long    i;
  46.     char    *null;
  47.     char    *string;
  48.     long    end;
  49. } dom = { 0l, "367", 0, ENDVAL };
  50.  
  51. static    struct    indom {
  52.     long        i;
  53.     long        systemtextlen;
  54.     struct    varlena    vlena;
  55. };
  56.  
  57. TestMain()
  58. {
  59.     static    int    existant = 0;
  60.     int        i, j;
  61.     ItemPointerData    tid;
  62.     char        *dat;
  63.  
  64.     struct    reldesc    *amopenr();
  65.     extern        heap_create();
  66.     extern        iinsert(), iprinttup(), showrelall();
  67.  
  68.     StartTransactionCommand();
  69.     startmmgr(0);
  70.     if (!existant) {
  71.         existant = 1;
  72.         heap_create(Relname, 'n', NATTS, DEFAULT_SMGR, Att);
  73.         CommitTransactionCommand();
  74.         StartTransactionCommand();
  75.     }
  76.     Reldesc = amopenr(Relname);
  77.     puts("Insert a list of numbers (<0)? (page,line for tid)");
  78.     for (;;) {
  79.         if (scanf("%ld", &i) != 1)
  80.             break;
  81.         startmmgr(1);
  82.         if (i < 0)
  83.             iinsert(-i);
  84.         else {
  85.             if (scanf(",%d", &j) != 1)
  86.                 break;
  87.             ItemPointerSimpleSet(&tid, i, j);
  88.             iprinttup(&tid);
  89.         }
  90.         endmmgr(NULL);
  91.         CommitTransactionCommand();
  92.         StartTransactionCommand();
  93.     }
  94.     puts("bye...");
  95.     showrelall();
  96.     endmmgr(NULL);
  97.     CommitTransactionCommand();
  98. }
  99.  
  100. iinsert(i)
  101. int    i;
  102. {
  103.     int        j;
  104.     char        *s;
  105.     struct    tuple    *tup;
  106.     struct    tuple    *addtupleheader();
  107.     struct    tuple    *formtuple();
  108.     RuleLock    aminsert();
  109.     struct    varlena    *textin();
  110.     extern        pfree();
  111.  
  112.     if (i & 01)
  113.         tup = addtupleheader(1, 4, (char *)&i);
  114.     else {
  115.         dom.i = i;
  116.         s = palloc(1 + i / 2);
  117.         for (j = 0; j < i / 2; j++)
  118.             s[j] = '1' + j % 7;
  119.         s[j] = '\0';
  120.         dom.string = (char *)textin(s);
  121.         pfree(s);
  122.         tup = formtuple(4, Att, (char *)&dom, Null);
  123.     }
  124.     aminsert(Reldesc, tup, (double *)NULL);
  125.     printf("inserted id %ld (length %lu) at %ld,%d\n", i, tup->t_len,
  126.         ItemPointerGetBlockNumber(&tup->t_ctid),
  127.         ItemPointerSimpleGetOffsetNumber(&tup->t_ctid));
  128.     pfree((char *)tup);
  129. }
  130.  
  131. iprinttup(tid)
  132. ItemPointer    tid;
  133. {
  134.     int        j;
  135.     unsigned    iden;
  136.     char        *s;
  137.     struct    tuple    *tup;
  138.     Buffer        b;
  139.     Boolean        isnull;
  140.     TUPLE        *amgetunique();
  141.     char        *getstruct();
  142.     char        *amgetattr();
  143.  
  144.     if ((tup = (struct tuple *)amgetunique(Reldesc, NowTimeQual, tid, &b))
  145.             != NULL) {
  146.         (void)amgetattr(tup, b, 2, Att, &isnull);
  147.         if (!isnull) {
  148.             fputs("Non-null second attribute!", stderr);
  149.             exitpg(1);
  150.         }
  151.         (void)amgetattr(tup, b, 3, Att, &isnull);
  152.         if (isnull) {
  153.             iden = ((struct indom *)getstruct(tup))->i;
  154.             printf("Retrieved %ld from %ld,%d\n", iden,
  155.                 ItemPointerGetBlockNumber(tid),
  156.                 ItemPointerSimpleGetOffsetNumber(tid));
  157.         } else {
  158.             iden = (long)amgetattr(tup, b, 1, Att, &isnull);
  159.             /* check isnull */
  160.             printf("Retrieved %ld (length %lu) from %ld,%d\n",
  161.                 iden, tup->t_len,
  162.                 ItemPointerGetBlockNumber(tid),
  163.                 ItemPointerSimpleGetOffsetNumber(tid));
  164.             if ((short)amgetattr(tup, b, 4, Att, &isnull) !=
  165.                     (short)ENDVAL)
  166.                 /* check isnull */
  167.                 fprintf(stderr, "Fourth attribute is %d!\n",
  168.                     amgetattr(tup, b, 4, Att, &isnull));
  169.                 /* check isnull */
  170.             s = amgetattr(tup, b, 3, Att, &isnull);
  171.             /* check isnull */
  172.             if (sizeof (long) + iden / 2 !=
  173.                     ((struct varlena *)s)->vl_len ||
  174.                     sizeof (long) + iden / 2 !=
  175.                     psize(s)) {
  176.                 printf("*** Lengths %ld, %ld, %ld\n",
  177.                     sizeof (long) + iden / 2, psize(s),
  178.                     ((struct varlena *)s)->vl_len);
  179.                 return;
  180.             }
  181.             s += sizeof (long);
  182.             for (j = 0; j < iden / 2; j++, s++)
  183.                 if (*s - j % 7 != '1')
  184.                     printf("data[%d] is %c not %c.\n",
  185.                            j, *s, '1' + j % 7);
  186.             ReleaseBuffer(b);
  187.         }
  188.     } else
  189.         printf("No tuple at %d\n", tid);
  190. }
  191.  
  192. showrelall()
  193. {
  194.     struct    relscan    *sdesc;
  195.     struct    tuple    *tup;
  196.     Buffer        b;
  197.     Boolean        isnull;
  198.     struct    relscan    *ambeginscan();
  199.     struct    tuple    *amgetnext();
  200.     extern        amendscan();
  201.     char        *getstruct();
  202.     extern        pfree();
  203.  
  204.     sdesc = ambeginscan(Reldesc, 0, NowTimeQual, 0, (struct skey *)NULL);
  205.     startmmgr(0);
  206.     while ((tup = amgetnext(sdesc, 0, &b)) != (struct tuple *)NULL) {
  207.         (void)amgetattr(tup, b, 3, Att, &isnull);
  208.         if (isnull) {
  209.             printf("id %ld at %ld,%d\n",
  210.                 ((struct indom *)getstruct(tup))->i,
  211.                 ItemPointerGetBlockNumber(&tup->t_ctid),
  212.                 ItemPointerSimpleGetOffsetNumber(&tup->t_ctid));
  213.         } else {
  214.             OID    id;
  215.  
  216.             id = (OID)amgetattr(tup, b, 1, Att, &isnull);
  217.             if (isnull) {
  218.                 printf("id *NULL* ???\n");
  219.             } else {
  220.                 printf("id %ld (length %lu) at %ld,%d\n",
  221.                     (long)id, tup->t_len,
  222.                     ItemPointerGetBlockNumber(&tup->t_ctid),
  223.                     ItemPointerSimpleGetOffsetNumber(&tup->t_ctid));
  224.             }
  225.         }
  226.         endmmgr(NULL);
  227.         startmmgr(0);
  228.     }
  229.     endmmgr(NULL);
  230.     amendscan(sdesc);
  231. }
  232.